package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Users;

import java.io.*;

/**
 * 用来处理和用户相关的业务操作
 */
public class UserController {
    //保存所有用户信息的目录的名字
    private static  String userDirName="./users/";
    static {
        //程序加载时判断一下保存所有的用户信息的目录是否存在，不存在先自动创建出来
        File userDir=new File(userDirName);
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }

    /**
     * 用来处理用户注册操作
     * @param request
     * @param response
     */
    public void reg(HttpRequest request, HttpResponse response){
        System.out.println("开始处理用户注册");
        //1从request中获取用户表单上提交的注册信息
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String nickname=request.getParameter("nickname");
        String ageStr=request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);
        //必要的验证工作，保证注册的四个信息不为空，否则值响应注册失败的提示页面
        //失败页面：reg_error.html居中显示一行子：注册失败，输入信息有误，请重新注册
        if(ageStr!=null&&ageStr.matches("[1-9]{1,2}|100")&&username!=null&&password!=null&&nickname!=null){
            /*
            判断是否为重复用户，如果是重复用户则直接响应页面：reg_have_user.html
            该页面剧中显示一行字：该用户已存在，请重新注册

            * */
            if(new File(userDirName+username+".obj").exists()){
                System.out.println("用户已经存在");
                response.setEntity(new File("./webapps/myweb/reg_have_user.html"));
                return;
            }
        int age=Integer.parseInt(ageStr);
        //2将该用户信息写入磁盘保存
        Users users=new Users(username,password,nickname,age);
        try (
                ObjectOutputStream oos=new ObjectOutputStream(
                        new FileOutputStream(userDirName+username+".obj")
                )
                ){
            oos.writeObject(users);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //3设置response响应注册结果页面
        response.setEntity(new File("./webapps/myweb/reg_success.html"));
        System.out.println("用户注册处理完毕");
        }else {


            response.setEntity(new File("./webapps/myweb/reg_fail.html"));
            System.out.println("用户注册失败》》》》");
        }


    }
    public void login(HttpRequest request, HttpResponse response)  {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        if(username==null||password==null){
            response.setEntity(new File("./webapps/myweb/login_error.html"));
            System.out.println("输入信息有误。。。。");
            return;
        }
        File file=new File(userDirName+username+".obj");
        if(file.exists()){
           try( FileInputStream fi=new FileInputStream(file);
            ObjectInputStream oi=new ObjectInputStream(fi)) {
               Users users = (Users) oi.readObject();
               if (password.equals(users.getPassword())) {
                   response.setEntity(new File("./webapps/myweb/login_success.html"));
                   return;
               }

           } catch (FileNotFoundException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           }
        }
        response.setEntity(new File("./webapps/myweb/login_fail.html"));

    }
}
